layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    form.on("radio(type)",function(data){
        if(data.elem.value == "1"){
            //转载
            $(".reprintUrl").removeClass("layui-hide");
            $(".reprintUrl :input").attr("lay-verify","required");
        }else{
            $(".reprintUrl").addClass("layui-hide");
            $(".reprintUrl :input").removeAttr("lay-verify");
            /*$(".originurl #type").removeAttr("lay-verify");*/
        }
    });

    form.verify({
        title : function(val){
            if(val == ''){
                return "文章标题不能为空";
            }
        },typeId : function(val){
            //这里有一个约定，下拉会出现请选择，请选择的value=-1 所以后台的数据中字典对应的值不能设置为-1
            if(val == '-1'){
                return "分类必选";
            }
        }

    })

    form.on("submit(addArtical)",function(data){
        //弹出loading
        var index = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.ajax({
            type: "post",
            url:"/manage/artical/addData",
            dataType: "json",
            data: $("#submitForm").serialize(),
            success: function (result) {
                if(result.flag == "success"){
                    layer.close(index);
                    top.layer.msg("文章添加成功！", {icon: 1});
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();

                }else{
                    layer.msg("添加失败", {icon: 2});
                }
            }, error: function () {
                layer.msg("添加失败", {icon: 2});
            }

        });
    })

    form.on("submit(updateArtical)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        // 实际使用时的提交信息
        $.ajax({
            type: "post",
            url:"/manage/artical/updateData",
            dataType: "json",
            data: $("#submitForm").serialize(),
            success: function (msg) {
                top.layer.close(index);
                top.layer.msg("文章更新成功！" , {icon: 1});
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }

        });
    })


    form.on("submit(updateContent)",function(data){
        //弹出loading
        var index = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.ajax({
            type: "post",
            url:"/manage/artical/updateContent",
            dataType: "json",
            data: $("#contentForm").serialize(),
            success: function (result) {
                if(result.flag == "success"){
                    layer.close(index);
                    top.layer.msg("保存成功！" , {icon: 1});


                }else{
                    layer.msg("保存失败", {icon: 2});
                }
            }, error: function () {
                layer.msg("保存失败", {icon: 2});
            }

        });
    })



    //关闭
    $(".cancel").on("click",function(){
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    });



})